package edu.iul.daam.rap.view.rss;

import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class ImageParser {
	//	private String rssResult = "";
	private boolean item = false;
	private boolean description = false;
	private boolean title = false;
	private boolean date = false;
	private final static String item_label = "item";
	private final static String link_label = "media:content";
	private final static String description_label = "media:description";
	private final static String title_label = "title";
	private final static String date_label = "dc:date.Taken";
	private final static String link_code_attribute_tag = "url";
	private ImageUnit image;
	private LinkedList<ImageUnit> images_box = new LinkedList<ImageUnit>();



	public LinkedList<ImageUnit> parse (String url) {

		try {

			URL feed_url= new URL(url);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			XMLReader xmlReader = saxParser.getXMLReader();
			RSSHandler rssHandler = new RSSHandler();
			xmlReader.setContentHandler(rssHandler);
			InputSource inputSource = new InputSource(feed_url.openStream());
			xmlReader.parse(inputSource);
			//            rssHandler.startElement(uri, localName, qName, attrs);

		} catch (IOException e) {System.err.println(e.getMessage());
		} catch (SAXException e) {System.err.println(e.getMessage());
		} catch (ParserConfigurationException e) {System.err.println(e.getMessage());
		}

		return images_box;
	}
	/**public String removeSpaces(String s) {
          StringTokenizer st = new StringTokenizer(s," ",false);
          String t="";
          while (st.hasMoreElements()) t += st.nextElement();
          return t;
        }*/
	private class RSSHandler extends DefaultHandler {

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attrs) throws SAXException {

			//						System.out.println("> "+qName);

			if (qName.equals(item_label)){
				item = true;
				image = new ImageUnit();
			}else
				if (qName.equals(title_label))
					title = true;
				else
					if (qName.equals(link_label)){
						image.setLink(attrs.getValue(link_code_attribute_tag));
					}else
						if (qName.equals(description_label))
							description = true;
						else
							if (qName.equals(date_label))
								date = true;

			/**if (!localName.equals("item") && item == true)
				rssResult = rssResult + localName + ": ";
			 **/
			//			System.out.println("1- "+uri);
			//			System.out.println("2- "+localName);
			//			System.out.println("2- "+qName);

		}

		@Override
		public void endElement(String namespaceURI, String localName,
				String qName) throws SAXException {

			//						System.out.println(qName);

			if (qName.equals(item_label)){
				item = false;
				images_box.add(image);
				//				System.out.println(image.toString());
				image=null;
			} else
				if (qName.equals(title_label))
					title = false;
				else
					if (qName.equals(description_label))
						description = false;
					else
						if (qName.equals(date_label))
							date = false;
		}


		public void characters(char[] ch, int start, int length)
				throws SAXException {
			String data = new String(ch, start, length);

			//			System.out.println("> start="+start+" length="+length+" "+new String(ch));
			/**
			if (item == true)
				rssResult = rssResult +(data.trim()).replaceAll("\\s+", " ")+"\t";
//			System.out.println("Evento3");
//			System.out.println("-->"+cdata);

			System.out.println("1- "+new String(ch, start, length));
			System.out.println("2- "+start);
			System.out.println("2- "+length);**/


			if (item){
				if (title){
					image.setTitle(data);
					//					System.out.println("1-"+data);
				}else
					if (description){
						image.setDescription(data);
						//							System.out.println("3-"+data);
					}else
						if (date){
							image.setDate(data);
							//																System.out.println("4-"+data);
						}
			}
		}

	}

	/**public static void main (String[] args){
		ImageParser image = new ImageParser();
		//		text.analisa();

		String site = "http://api.flickr.com/services/feeds/photos_public.gne?tags=portugal&format=rss_200";
		String site2 = "http://services.sapo.pt/RSS/Feed/noticias/homepage_geral";
//		image.parse(site);
				System.out.println(image.parse(site));
	}**/

}